import java.util.HashMap;
import java.util.Map;

public class test3 {
    //力扣 76.最小覆盖子串
    public static void main(String[] args) {
        String s = "cabwefgewcwaefgcf", t = "cae";
        System.out.println(minWindow(s, t));
    }
    public static String minWindow(String ss, String tt) {

        String ret = "";
        int min = ss.length()+1;
        char[] s = ss.toCharArray();
        char[] t = tt.toCharArray();

        Map<Character,Integer> hash1 = new HashMap<>();
        for (char c:t) {
            hash1.put(c,hash1.getOrDefault(c,0)+1);
        }
        int n = t.length;

        Map<Character,Integer> hash2 = new HashMap<>();

        for (int left = 0,right = 0,count = 0;right<s.length;right++){
            char in = s[right];
            hash2.put(in,hash2.getOrDefault(in,0)+1);
            if(hash2.get(in)<=hash1.getOrDefault(in,0)){
                count++;
            }
            while (count==n){
                if(min>right-left+1){
                    ret = ss.substring(left,right+1);
                    min=right-left+1;
                }

                char out = s[left];
                if(hash2.get(out)<=hash1.getOrDefault(out,0)){
                    count--;
                }
                left++;
                int tmp = hash2.get(out);
                hash2.put(out,tmp-1);

            }
        }
        return ret;
    }
}
